﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EM.Components.DataAccess;
using EM.Components.Entities;
using System.Threading.Tasks;

namespace EM.Components.Business
{
    public class CampaignProvider
    {
        private CampaignDataAccess _da;
        public CampaignProvider() 
        {
            _da = new CampaignDataAccess();
        }

        public List<EM_Campaigns> GetAll()
        {
            return _da.GetAll();
        }

        public List<EM_Campaigns> GetActive()
        {
            var all = GetAll();
            var active = from c in all
                         where c.Approved == true
                         orderby c.StartDate descending
                         select c;
            return active.ToList<EM_Campaigns>();
        }

        public List<EM_Campaigns> GetPending() 
        {
            var all = GetAll();
            var pending = from c in all
                         where c.Submitted==true && c.Approved != true
                         orderby c.StartDate descending
                         select c;
            return pending.ToList<EM_Campaigns>(); 
        }

        public List<EM_Campaigns> GetDraft() 
        {
            var all = GetAll();
            var draft = from c in all
                          where c.Submitted != true
                          orderby c.StartDate descending
                          select c;
            return draft.ToList<EM_Campaigns>(); 
        }

        public EM_Campaigns Get(int entityId) 
        {
            return _da.Get(entityId);
        }

        /// <summary>
        /// Add new campaign
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public EM_Campaigns Add(EM_Campaigns entity) 
        {
            if (_da.Get(entity.CampaignName) != null)
            {
                throw new ArgumentException("Campaign name already exist " + entity.CampaignName + " is already used.");
            }
            else 
            {
                _da.Add(entity);
            }
            return entity;
        }

        public EM_Campaigns Update(EM_Campaigns entity) 
        {
            return _da.Update(entity);
        }

        public void Submit(EM_Campaigns entity) 
        {
            Submit(entity.CampaignID);
        }

        public void Submit(int entityId) 
        {
            var entity = Get(entityId);
            entity.Submitted = true;
            Update(entity);
        }

        public void Approve(int entityId, string user) 
        {
            var entity = Get(entityId);
            entity.Approved = true;
            entity.ApprovedBy = user;
            entity.ApprovedDate = DateTime.Now;
            Update(entity);


            //_da.CreateCampaignInstances(entity.EM_EmailInstances.FirstOrDefault<EM_EmailInstances>().EmailInstanceID);
            int eid = entity.EM_EmailInstances.FirstOrDefault<EM_EmailInstances>().EmailInstanceID;
            try
            {
                Task t = new Task(() =>
                { _da.CreateCampaignInstances(eid); });
                t.Start();
            }
            catch
            {
                throw new Exception("Busy");
            }
        }

        public void Delete(EM_Campaigns entity) 
        {
            Delete(entity.CampaignID);
        }

        public void Delete(int entityId) 
        {
            _da.Delete(entityId);
        }
    }
}
